package com.alone.jpa.util;

import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import lombok.experimental.UtilityClass;
import org.springframework.data.domain.Sort;

import java.util.Arrays;

/**
 * @author Alone
 */
@UtilityClass
public class Sorts {

    @SafeVarargs
    public <T> Sort by(Sort.Direction direction, Func1<T, ?>... properties) {
        return Sort.by(direction, Arrays.stream(properties).map(LambdaUtil::getFieldName).toArray(String[]::new));
    }

    @SafeVarargs
    public <T> Sort by(Func1<T, ?>... properties) {
        return Sort.by(Arrays.stream(properties).map(LambdaUtil::getFieldName).toArray(String[]::new));
    }

}
